using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class CC2ScriptHandle : ScriptHandleBase
{
    /// <summary>
    /// 导出正常版代码
    /// </summary>
    public override void ExportScript(List<ExcelData> list, string path)
    {
        ExportClassScript(list, path);
        ExportLoaderScript(list, path, "/CC2Template2.txt", "/CC2AnalysisTemplate2.txt");
    }

    /// <summary>
    /// 导出压缩版代码
    /// </summary>
    public override void ExportCompressScript(List<ExcelData> list, string path)
    {
        ExportClassScript(list, path);
        ExportLoaderScript(list, path, "/CC2Template.txt", "/CC2AnalysisTemplate.txt", true);
    }

    /// <summary>
    /// 导出Config类代码
    /// </summary>
    public override void ExportClassScript(List<ExcelData> list, string path)
    {
        for (int i = 0; i < list.Count; i++)
        {
            ExcelData data = list[i];

            string script_str = $"export class {data.name}" + " {\n";
            for (int j = 0; j < data.types.Count; j++)
            {
                script_str += $"\tpublic {data.fields[j]}: {data.types[j]};\n";
            }
            script_str += "}";
            FileHandle.SaveFile(script_str, path + data.name + ".ts");
            LogTool.WriteLog($"导出代码: {data.name}.ts");
        }
    }

    /// <summary>
    /// 导出Loader代码
    /// </summary>
    public void ExportLoaderScript(List<ExcelData> list, string path, string pathLoader, string pathAnalysis, bool isCompress = false)
    {
        StreamReader reader = new StreamReader(Application.streamingAssetsPath + pathLoader);
        string loader = reader.ReadToEnd();
        reader.Close();

        reader = new StreamReader(Application.streamingAssetsPath + pathAnalysis);
        string analysis = reader.ReadToEnd();
        reader.Close();

        string config_import = "";
        string config_map = "";
        string config_analysis = "";
        string cc = isCompress ? "\t\t\t\t\t" : "\t\t\t\t\t\t";
        for (int i = 0; i < list.Count; i++)
        {
            ExcelData data = list[i];

            config_import += "import { {0} } from \"./{0}\";\n".Replace("{0}", data.name);
            config_map += $"\tpublic static {data.name}_Map: Map<{data.key_type}, {data.name}>;\n";

            string content = analysis;
            string value = "";
            for (int j = 0; j < data.fields.Count; j++)
            {
                value += cc + $"item.{data.fields[j]}=temp[{j}];\n";
            }
            content = content.Replace("{keyType}", data.key_type);
            content = content.Replace("{config}", data.name);
            content = content.Replace("{key}", data.key);
            content = content.Replace("{value}", value.Substring(0, value.Length - 1));
            config_analysis += "\n" + content;
        }

        loader = loader.Replace("{config_count}", list.Count.ToString());
        loader = loader.Replace("{config_import}", config_import);
        loader = loader.Replace("{config_map}", config_map);
        loader = loader.Replace("{config_analysis}", config_analysis);

        FileHandle.SaveFile(loader, path + "JsonLoader.ts");
        LogTool.WriteLog($"导出代码: JsonLoader.ts");
    }
}
